package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.util.DateUtil;
import biz.appframe.PageJsonBean;

import com.rfid.project.exception.CannotUpdateException;
import com.rfid.project.model.Project;
import com.rfid.project.model.ProjectTaskDelayApply;
import com.rfid.project.model.ProjectTaskPerson;
import com.rfid.project.service.ProcessStatus;
import com.rfid.project.service.ProjectService;
import com.rfid.project.service.ProjectTaskDelayApplyService;
import com.rfid.project.service.ProjectTaskPersonService;
import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;
import com.rfid.project.model.ProjectTask;
import com.rfid.project.service.ProjectTaskService;

@Controller
public class ProjectTaskDelayApplyController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProjectTaskDelayApplyController.class);

	@Autowired
	private ProjectTaskDelayApplyService projectTaskDelayApplyService;

	@Autowired
	private PeopleService peopleService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private ProjectTaskPersonService projectTaskPersonService;

	@RequestMapping(value = "/projectTaskDelayApplymgr")
	public String projectTaskDelayApplymgr(HttpServletRequest request,
			HttpServletResponse response, Model model, @Valid ProjectTaskPerson projectTaskPerson) {
		projectTaskPerson = this.projectTaskPersonService.findOne(projectTaskPerson.getId());
		model.addAttribute("projectTaskPerson", projectTaskPerson);
		return "projectTaskDelayApplymgr";
	}

	@RequestMapping(value = "/queryProjectTaskDelayApply")
	public String queryProjectTaskDelayApply(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "projectTaskPersonId", defaultValue = "-1") Long projectTaskPersonId,
			@RequestParam(value = "projectId", defaultValue = "-1") Long projectId,
			@RequestParam(value = "applyPersonId", defaultValue = "-1") Long applyPersonId,
			@RequestParam(value = "applyReason", defaultValue = "") String applyReason,
			@RequestParam(value = "checkPersonId", defaultValue = "-1") Long checkPersonId,
			@RequestParam(value = "checkComment", defaultValue = "") String checkComment,
			@RequestParam(value = "applyTimeBegin", defaultValue = "") Date applyTimeBegin,
			@RequestParam(value = "applyTimeEnd", defaultValue = "") Date applyTimeEnd,
			@RequestParam(value = "checkTimeBegin", defaultValue = "") Date checkTimeBegin,
			@RequestParam(value = "checkTimeEnd", defaultValue = "") Date checkTimeEnd,
			@RequestParam(value = "status", defaultValue = "") String status,
			@RequestParam(value = "originalPlanFinishDateBegin", defaultValue = "") Date originalPlanFinishDateBegin,
			@RequestParam(value = "originalPlanFinishDateEnd", defaultValue = "") Date originalPlanFinishDateEnd,
			@RequestParam(value = "targetPlanFinishDateBegin", defaultValue = "") Date targetPlanFinishDateBegin,
			@RequestParam(value = "targetPlanFinishDateEnd", defaultValue = "") Date targetPlanFinishDateEnd,
			@RequestParam(value = "projectTaskId", defaultValue = "-1") Long projectTaskId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<ProjectTaskDelayApply> projectTaskDelayApplys = projectTaskDelayApplyService
				.findAll(projectId, projectTaskPersonId, applyPersonId, applyReason, checkPersonId,
						checkComment, applyTimeBegin, applyTimeEnd,
						checkTimeBegin, checkTimeEnd, status,
						originalPlanFinishDateBegin, originalPlanFinishDateEnd,
						targetPlanFinishDateBegin, targetPlanFinishDateEnd,
						projectTaskId, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", projectTaskDelayApplys.getTotalElements());
		JSONArray models = new JSONArray();
		for (ProjectTaskDelayApply item : projectTaskDelayApplys) {
			JSONObject mod = new JSONObject();
			wrapData(item, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findProjectTaskDelayApply")
	public String findProjectTaskDelayApply(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id,
			Model model) {
		ProjectTaskDelayApply projectTaskDelayApply = projectTaskDelayApplyService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(projectTaskDelayApply, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(ProjectTaskDelayApply item, JSONObject mod) {
		mod.put("id", item.getId());
		mod.put("status", ProcessStatus.processStatusName(item.getStatus()));
		mod.put("statusCode", item.getStatus());
		mod.put("originalPlanFinishDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getOriginalPlanFinishDate()));
		mod.put("targetPlanFinishDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getTargetPlanFinishDate()));
		mod.put("applyTime", com.rfid.project.util.DateUtil.dateTheDayToString(item.getApplyTime()));
		mod.put("applyReason", item.getApplyReason());
		mod.put("checkTime", com.rfid.project.util.DateUtil.dateTheDayToString(item.getCheckTime()));

		String checkContent = item.getCheckComment();
		mod.put("checkComment", checkContent == null ? "" : checkContent);
		mod.put("projectTaskId", item.getProjectTaskPerson().getProjectTask().getId());
		mod.put("projectTaskPersonId", item.getProjectTaskPerson().getId());
		mod.put("projectTaskPersonDuty", item.getProjectTaskPerson().getDuty());
		mod.put("projectTaskTaskDescription", item.getProjectTaskPerson().getProjectTask().getTaskDescription());
		mod.put("projectId", item.getProjectTaskPerson().getProjectTask().getProject().getId());
		mod.put("projectName", item.getProjectTaskPerson().getProjectTask().getProject().getName());
		mod.put("applyPersonId", item.getApplyPerson().getId());
		mod.put("applyPersonName", item.getApplyPerson().getName());

		People checkPerson = item.getCheckPerson();
		if (null == checkPerson) {
			mod.put("checkPersonId", -1);
			mod.put("checkPersonName", "");
		} else {
			mod.put("checkPersonId", checkPerson.getId());
			mod.put("checkPersonName", checkPerson.getName());
		}
	}

	@RequestMapping(value = "/getProjectTaskDelayApply")
	public String getProjectTaskDelayApply(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id) {
		ProjectTaskDelayApply projectTaskDelayApply = projectTaskDelayApplyService.findOne(id);
		JSONObject mod = new JSONObject();
		wrapData(projectTaskDelayApply, mod);
		PageJsonBean.printObject(response, mod);
		return null;
	}

	@RequestMapping(value = "/editProjectTaskDelayApply", method = RequestMethod.POST)
	public String editProjectTaskDelayApply(HttpServletRequest request,
			HttpServletResponse response,
			@Valid ProjectTaskDelayApply projectTaskDelayApply, Model model,
			RedirectAttributes redirectAttributes) {
		People applyPerson = projectTaskDelayApply.getApplyPerson();
		if(null == applyPerson || null == applyPerson.getId()){
			projectTaskDelayApply.setApplyPerson((People)request.getSession().getAttribute("currentUser"));
		}
		projectTaskDelayApplyService.saveProjectTaskDelayApply(projectTaskDelayApply);
		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/checkProjectTaskDelayApply", method = RequestMethod.POST)
	public String checkProjectTaskDelayApply(
			HttpServletRequest request,
			HttpServletResponse response,
			@Valid ProjectTaskDelayApply projectTaskDelayApply,
			Model model, RedirectAttributes redirectAttributes) {
		People currentUser = (People) request.getSession().getAttribute("currentUser");
		JSONObject result = new JSONObject();
		if (projectTaskDelayApply != null && currentUser != null) {
			projectTaskDelayApply.setCheckPerson(currentUser);
		}
		boolean flag = true;
		try {
			this.projectTaskDelayApplyService.checkProjectTaskDelayApply(projectTaskDelayApply);
		} catch (CannotUpdateException e) {
			flag = false;
			result.put("msg", e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@RequestMapping(value = "/delProjectTaskDelayApply")
	public String delProjectTaskDelayApply(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.projectTaskDelayApplyService.deleteProjectTaskDelayApply(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}
